package by.grsu.hdm;

import java.util.ArrayList;
import java.util.List;

import by.grsu.hdm.devices.MediaCenter;
import by.grsu.hdm.devices.NetworkMonitor;
import by.grsu.hdm.devices.UsbFlashDrive;
import by.grsu.hdm.devices.WashingMachine;
import by.grsu.hdm.model.HomeDevice;
import by.grsu.hdm.model.IDevice;
import by.grsu.hdm.model.IHomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.reporting.TextReportBuilder;
import by.grsu.hdm.reporting.TrafficReportData;

import java.lang.Exception;

public class HomeDeviceManagerStarter {

	public static void main(String[] args) {

		List<IHomeDevice> devices = new ArrayList<IHomeDevice>();

		MediaCenter media = new MediaCenter("MEDIA#132456");
		UsbFlashDrive flash = new UsbFlashDrive("FLASH#32"); 
		media.setUsbPort("usbPortTralala");
		media.getUSBPort().connectDevice(flash);

		devices.add(media);
		devices.add(flash);
		devices.add(new WashingMachine("WASHING#45"));

		// turn all devices on..
		for (IHomeDevice device : devices) {
			device.turnOn();
		}
		media.contactMediaPlayer().remote.play(media.contactMediaPlayer().audioList.get(0)); 
		
		// status all devices:
		for (IHomeDevice device : devices) {
			System.out.println( ((HomeDevice)device).getHomeDeviceStatus().getStatus() );
		}
		
		

		// check for connected devices
		/*for (IDevice device : devices) {
			if (device instanceof IUsbSupport) {
				IUsbPort usb = ((IUsbSupport) device).getUsbPort();
				if (usb.hasConnectedDevice()) {
					IDevice usbDevice = usb.getConnectedDevice();

					if (usbDevice != null)
						System.out.println(usbDevice.getDeviceId()
								+ " is connected to " + device.getDeviceId());
				}
			}
		}*/

		// register devices for network monitoring
		NetworkMonitor monitor = new NetworkMonitor();

		for (IDevice device : devices) {
			if (device instanceof INetworkAvailable) {
				monitor.registerDevice((INetworkAvailable) device);
			}
		}

		TrafficReportData reportData = monitor.getNetworkStatistics();

		TextReportBuilder text = new TextReportBuilder(reportData);
		String report = text.buildReport();

		System.out.println(report);

		// turn all devices off..
		for (IHomeDevice device : devices) {
			device.turnOff();
		}
	}

}
